package com.backend.thread.countDownLatch

import java.util.*
import java.util.concurrent.CountDownLatch
import java.util.concurrent.Executors


object Application {
    private var services: MutableList<BaseHealthChecker> = ArrayList()

    private var latch: CountDownLatch = CountDownLatch(2)

    @JvmStatic
    fun main(args: Array<String>) {
        var result = false
        try {
            result = checkExternalServices()
        } catch (e: Exception) {
            e.printStackTrace()
        }
        println("External services validation completed !! Result was :: " + result)
    }

    @Throws(Exception::class)
    private fun checkExternalServices(): Boolean {
        services.add(NetworkHealthChecker(latch))
        services.add(DataBaseHealthChecker(latch))

        val executor = Executors.newFixedThreadPool(services.size)

        for (v in services) {
            executor.execute(v)
        }

        latch.await()

        return services.any { it.isServiceUp }
    }


}

